//
//  MadDatabase.h
//  MadGTD
//
//  Created by Fuzhou Chen on 4/29/12.
//  Copyright (c) 2012 Fuzhou Chen. All rights reserved.
//

#ifndef MadGTD_MadDatabase_h
#define MadGTD_MadDatabase_h

#include <stdint.h>
#include "MadDebug.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef enum _MadConstants
{
    MAD_MAX_STRING_LEN = 128,
    MAD_MAX_TODOS = 65535,
    MAD_MAX_TAGS = 256,
} MadConstants;   
typedef enum _MadDbQueryFlag {
    MAD_DB_QUERY_ACTIVE_TODOS = 0x01,
    MAD_DB_QUERY_DONE_TODOS   = 0x02,
} MadDbQueryFlag;

struct _MadDatabase;
typedef struct _MadDatabase   MadDatabase;
typedef struct _MadDTBlob MadDTBlob;
typedef MadDTBlob          MadUniqueId;

typedef struct _MadTodo
{
    MadUniqueId*         objId;
    char*                content;
    time_t               dueDateTime;
    time_t               completeDateTime;
    time_t               lastUpdateDateTime;
} MadTodo;
typedef struct _MadTag
{
    MadUniqueId*         objId;
    char*                name;
    uint32_t             mark;
    time_t               lastUpdateDateTime;
} MadTag;

/// Methods for MadDatabase
extern MadResult
MadDbOpen(const char* dbFileName, MadDatabase** outNewDb);
extern void
MadDbClose(MadDatabase* db);

extern MadResult
MadDbAddTodo(MadDatabase* db, MadTodo* todo);
extern MadResult
MadDbAddTag(MadDatabase* db, MadTag* tag);
extern MadResult
MadDbQueryTodos(MadDatabase* db, MadDbQueryFlag flag, MadTodo** outTodos);
extern MadResult
MadDbQueryTags(MadDatabase* db, MadTag** outTags);
extern MadResult
MadDbQueryTodosByTag(MadDatabase* db, MadTag* tag, MadTodo** outTodos);
extern MadResult
MadDbQueryTagsByTodo(MadDatabase* db, MadTodo* todo, MadTag** outTags);
extern MadResult
MadDbRemoveObjectInDb(MadDatabase* db, MadUniqueId* objId);
extern MadResult
MadDbAssignTag(MadDatabase* db, MadTodo* todo, MadTag* tag);
extern MadResult
MadDbUnassignTag(MadDatabase* db, MadTodo* todo, MadTag* tag);

extern MadResult MadDbGetQueryTodosCount(MadTodo* todos);
extern MadResult MadDbGetQueryTagsCount(MadTag* tags);
    
extern MadResult MadDbCalculateTodoMark(MadTodo* todo);

extern void
MadDbTodoQueryDispose(MadTodo* info);
extern void
MadDbTagQueryDispose(MadTag* info);

extern void
MadConvertUTC(MadDTBlob* dtBlob, time_t dtsec, suseconds_t msecs);
extern void
MadGetCurrentUTC(MadDTBlob* dtBlob);

#ifdef __cplusplus
}
#endif

#endif
